package com.zhoujing.cinema.front.controller;

import com.github.pagehelper.PageInfo;
import com.zhoujing.cinema.constants.Constant;
import com.zhoujing.cinema.entity.*;
import com.zhoujing.cinema.front.controller.VO.Actor;
import com.zhoujing.cinema.front.controller.VO.MovieVo;
import com.zhoujing.cinema.service.MovieService;
import com.zhoujing.cinema.utils.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：ywj
 * @date ：Created in 2020/3/18 14:40
 * @description：
 * @modified By：
 * @version: $
 */
@RequestMapping("/front/movie")
@Controller
public class MovieController {
    @Autowired
    public MovieService movieService;
    @RequestMapping("/{id}")
    @ResponseBody
    public ResponseResult getMovieById(@PathVariable Integer id) throws InvocationTargetException, IllegalAccessException {
        Movie movie = movieService.findMovieById(id);
        MovieVo movieVo = new MovieVo();
        BeanUtils.copyProperties(movie,movieVo);
        movieVo.setCountryName(movie.getCountry().getCountry_name());
        movieVo.setYearName(movie.getYear().getYear_name());
        movieVo.setTypeName(movie.getMovieType().getMov_type_name());
        String[] arr = movie.getPics().split(",");
        movieVo.setDirectorPic(arr[0]);
        ArrayList<String> arrPic = new ArrayList<String>();
        for(int i=0;i<=arr.length-1;i++){
            arrPic.add(i,arr[i]);
        }
        arrPic.remove(0);
        String actor[] = movie.getActor().split(",");
        String types[] = movie.getType().split(",");
        movieVo.setTypes(types);
        ArrayList<String> arrName = new ArrayList<String>();
        ArrayList<String> arrRoleName= new ArrayList<String>();
        for(int i = 0;i<=actor.length-1;i++){
            String arrActorName[] = actor[i].split("/");
            arrName.add(i,arrActorName[0]);
            arrRoleName.add(i,arrActorName[1]);
        }
        List<Actor> list = new ArrayList<>();
        for (int i =0;i<=actor.length-1;i++){
            Actor actor1 = new Actor(arrName.get(i),arrRoleName.get(i),arrPic.get(i));
            list.add(actor1);
        }
        movieVo.setActors(list);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(movieVo);
        responseResult.setStatus(Constant.RESPONSE_STATUS_SUCCESS);
        responseResult.setMessage("查询成功");
        return responseResult;
    }
    @RequestMapping("/movieDetail")
    public String getCommentByMovieId(Integer pageNum,Model model,@RequestParam("id") Integer id){
        if(ObjectUtils.isEmpty(pageNum)){
            pageNum=Constant.PAGE_NUM;
        }
        PageInfo<Comment> list = movieService.findCommentByMovieId(id,pageNum,Constant.PAGE_SIZE);
        model.addAttribute("data",list);
        return "movieDetail";
    }
    @RequestMapping("/checkWish")
    @ResponseBody
    public ResponseResult checkWish(@CookieValue(value = "userId")Integer userId){
        Wish wish = movieService.selectWish(userId);
        ResponseResult responseResult = new ResponseResult();
        if(wish==null){
            responseResult.setData(false);
        }else{
            responseResult.setData(true);
        }
        return responseResult;
    }
    @RequestMapping("/addWish")
    @ResponseBody
    public ResponseResult addWish(@CookieValue(value = "userId")Integer userId,Integer movieId){
        Wish wish = new Wish();
        wish.setMovie_id(movieId);
        wish.setUser_id(userId);
        movieService.insertWish(wish);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setStatus(Constant.RESPONSE_STATUS_SUCCESS);
        responseResult.setMessage("数据插入成功");
        return responseResult;
    }
    @RequestMapping("/modifyMovieWish")
    @ResponseBody
    public ResponseResult modifyMovieWish(String wish,Integer movieId){
        Movie movie = new Movie();
        movie.setWant_count(wish);
        movie.setId(movieId);
        movieService.updateMovieWish(movie);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setStatus(Constant.RESPONSE_STATUS_SUCCESS);
        responseResult.setMessage("更新插入成功");
        return responseResult;
    }
    @RequestMapping("/checkComment")
    @ResponseBody
    public ResponseResult checkComment(@CookieValue(value = "userId")Integer userId){
        Comment comment = movieService.selectComment(userId);
        ResponseResult responseResult = new ResponseResult();
        if(comment==null){
            responseResult.setData(false);
        }else{
            responseResult.setData(true);
        }
        return responseResult;
    }
    @RequestMapping("/addComment")
    @ResponseBody
    public ResponseResult addComment(@CookieValue(value = "userId")Integer userId,Integer movieId,String score,String comment,String create_time){
        CommentDto comment1 = new CommentDto();
        comment1.setComment(comment);
        comment1.setMovie_id(movieId);
        comment1.setScore(score);
        comment1.setCreate_time(create_time);
        comment1.setUser_id(userId);
        movieService.insertComment(comment1);
        List<Comment> list = movieService.selectCommentByMovie(movieId);
        Integer sum= 0;
        for(Comment c:list){
            sum+=Integer.parseInt(c.getScore());
        }
        double scoreNew = (double) sum/list.size();
        String sccc = String.format("%.1f", scoreNew);
        Movie movie = new Movie();
        movie.setScore(sccc);
        movie.setId(movieId);
        movieService.updateMovieScore(movie);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setStatus(Constant.RESPONSE_STATUS_SUCCESS);
        responseResult.setMessage("数据插入成功");
        return responseResult;
    }
    @RequestMapping("/getBanner")
    @ResponseBody
    public ResponseResult getBanner(){
        List<Banner> list = movieService.selectBanner();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(list);
        responseResult.setMessage("查询成功");
        responseResult.setStatus(Constant.RESPONSE_STATUS_SUCCESS);
        return responseResult;
    }
    @RequestMapping("/getHotPlay")
    @ResponseBody
    public ResponseResult getHotPlay(){
        List<Movie> list = movieService.findType1Eight();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(list);
        responseResult.setMessage("查询成功");
        responseResult.setStatus(Constant.RESPONSE_STATUS_SUCCESS);
        return responseResult;
    }
    @RequestMapping("/getHotPlayList")
    @ResponseBody
    public ResponseResult getHotPlayList(){
        List<Movie> list = movieService.findType1Ten();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(list);
        responseResult.setMessage("查询成功");
        responseResult.setStatus(Constant.RESPONSE_STATUS_SUCCESS);
        return responseResult;
    }
    @RequestMapping("/getPlanPlay")
    @ResponseBody
    public ResponseResult getPlanPlay(){
        List<Movie> list = movieService.findType2Eight();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(list);
        responseResult.setMessage("查询成功");
        responseResult.setStatus(Constant.RESPONSE_STATUS_SUCCESS);
        return responseResult;
    }
    @RequestMapping("/getPlanPlayList")
    @ResponseBody
    public ResponseResult getPlanPlayList(){
        List<Movie> list = movieService.findType2Ten();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(list);
        responseResult.setMessage("查询成功");
        responseResult.setStatus(Constant.RESPONSE_STATUS_SUCCESS);
        return responseResult;
    }
    @RequestMapping("/getGoodPlay")
    @ResponseBody
    public ResponseResult getGoodPlay(){
        List<Movie> list = movieService.findType3Eight();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(list);
        responseResult.setMessage("查询成功");
        responseResult.setStatus(Constant.RESPONSE_STATUS_SUCCESS);
        return responseResult;
    }
    @RequestMapping("/getGoodPlayList")
    @ResponseBody
    public ResponseResult getGoodPlayList(){
        List<Movie> list = movieService.findType3Ten();
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(list);
        responseResult.setMessage("查询成功");
        responseResult.setStatus(Constant.RESPONSE_STATUS_SUCCESS);
        return responseResult;
    }
    @RequestMapping("/search")
    public String goSearch(String kw,Integer pageNum,Model model){
        if(ObjectUtils.isEmpty(pageNum)){
            pageNum=Constant.PAGE_NUM;
        }
        PageInfo<Movie> list = movieService.findMovieSearch(kw,pageNum,6);
        model.addAttribute("data",list);
        return "search";
    }
    @RequestMapping("/movie")
    public String goMovie(Model model,Integer pageNum){
        if(ObjectUtils.isEmpty(pageNum)){
            pageNum=Constant.PAGE_NUM;
        }
        List<Type> listType = movieService.findType();
        List<Year> listYear = movieService.findYear();
        List<Country> listCountry = movieService.findCountry();
        PageInfo<Movie> hotList = movieService.findMovieType1(pageNum,6);
        PageInfo<Movie> planList = movieService.findMovieType2(pageNum,6);
        PageInfo<Movie> goodList = movieService.findMovieType3(pageNum,6);
        model.addAttribute("data1",listType);
        model.addAttribute("data2",listYear);
        model.addAttribute("data3",listCountry);
        model.addAttribute("hotList",hotList);
        model.addAttribute("planList",planList);
        model.addAttribute("goodList",goodList);
        return "movie";
    }
    @ResponseBody
    @RequestMapping("/searchWithQuery")
    public ResponseResult searchWithQuery(Integer country,Integer year,String type,Integer pageNum){
        MovieDto movieDto = new MovieDto();
        movieDto.setCountry(country);

        movieDto.setYear(year);
        if(ObjectUtils.isEmpty(pageNum)){
            pageNum=Constant.PAGE_NUM;
        }
        if (ObjectUtils.isEmpty(type)){
            type=null;
        }
        movieDto.setType(type);
        PageInfo<Movie> list = movieService.selectMovieWithQuery1(movieDto,pageNum,6);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(list);
        responseResult.setStatus(Constant.RESPONSE_STATUS_SUCCESS);
        responseResult.setMessage("查询成功");
        return responseResult;
    }
    @ResponseBody
    @RequestMapping("/searchWithQuery2")
    public ResponseResult searchWithQuery2(Integer country,Integer year,String type,Integer pageNum){
        MovieDto movieDto = new MovieDto();
        movieDto.setCountry(country);
        movieDto.setYear(year);
        if(ObjectUtils.isEmpty(pageNum)){
            pageNum=Constant.PAGE_NUM;
        }
        if (ObjectUtils.isEmpty(type)){
            type=null;
        }
        movieDto.setType(type);
        PageInfo<Movie> list = movieService.selectMovieWithQuery2(movieDto,pageNum,6);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(list);
        responseResult.setStatus(Constant.RESPONSE_STATUS_SUCCESS);
        responseResult.setMessage("查询成功");
        return responseResult;
    }
    @ResponseBody
    @RequestMapping("/searchWithQuery3")
    public ResponseResult searchWithQuery3(Integer country,Integer year,String type,Integer pageNum){
        MovieDto movieDto = new MovieDto();
        movieDto.setCountry(country);
        movieDto.setYear(year);
        if(ObjectUtils.isEmpty(pageNum)){
            pageNum=Constant.PAGE_NUM;
        }
        if (ObjectUtils.isEmpty(type)){
            type=null;
        }
        movieDto.setType(type);
        PageInfo<Movie> list = movieService.selectMovieWithQuery3(movieDto,pageNum,6);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(list);
        responseResult.setStatus(Constant.RESPONSE_STATUS_SUCCESS);
        responseResult.setMessage("查询成功");
        return responseResult;
    }
}
